rm(list=ls())
library(randomForestSRC)
library(randomForest)
library(tidyverse)
library(survival)
library(plyr)
library(dplyr)
library(foreign)
library(haven)
library(ggRandomForests)
library(survival)
library(ResourceSelection)

setwd("C:/Users/kaish/Documents/Academic Work/Writing/Covid Policy Diffusion/Randomforest")

dataCOVID <- read.csv("C:/Users/kaish/Documents/Academic Work/Writing/Covid Policy Diffusion/DataCOVID_randomforest_March2023_school.csv")

dataCOVID$des_school[dataCOVID$des_school %in% NA] <- 0
dataCOVID$des_work[dataCOVID$des_work %in% NA] <- 0
dataCOVID$des_public[dataCOVID$des_public %in% NA] <- 0
dataCOVID$des_domestic[dataCOVID$des_domestic %in% NA] <- 0
dataCOVID$des_international[dataCOVID$des_international %in% NA] <- 0

fitform <- Surv(time = dataCOVID$tf, event = dataCOVID$des_school) ~ dataCOVID$WHO_pandemic + dataCOVID$US_school + dataCOVID$China_unesco + dataCOVID$FDI_US + dataCOVID$BRI_country + 
    dataCOVID$GHI_prime + dataCOVID$airmil + dataCOVID$pop_older65 + dataCOVID$World_school + dataCOVID$region_school + dataCOVID$World_casethou + dataCOVID$World_deaththou + 
      dataCOVID$V4_Bin + dataCOVID$V8_Bin + dataCOVID$v2exl_legitideol + dataCOVID$v2exl_legitideolcr_0 + dataCOVID$v2exl_legitideolcr_1 + dataCOVID$v2exl_legitideolcr_2 + dataCOVID$v2exl_legitideolcr_3 + dataCOVID$v2exl_legitideolcr_4 + 
      dataCOVID$elections2020 + dataCOVID$v2x_freexp + dataCOVID$v2dlengage + dataCOVID$polyarchy_stddev + dataCOVID$COVID_casesthou + dataCOVID$v2x_polyarchy + dataCOVID$GDPmil + dataCOVID$HDI + dataCOVID$areamil + dataCOVID$popmil + 
      dataCOVID$v2xel_locelec + dataCOVID$v2xel_regelec + dataCOVID$women_in_parliament

fitcox <- coxph(fitform)
summary(fitform)

## Replace NAs in the des_ columns
dataCOVID <- 
  dataCOVID %>% 
  mutate(across(starts_with("des_"), ~replace_na(.x, 0)),
         tf = tf+67) # this line makes the minimum value of tf 0. Needs to be checked if that makes sense

## Replace NAs in the des_ columns
dataCOVID <- 
  dataCOVID %>% mutate(across(starts_with("des_"), ~replace_na(.x, 0))) %>% 
  filter(tf >-1)

dataCOVID <- dataCOVID %>% filter(tf >-1)

## Random forest
set.seed(1000)
train <- sample(1:19090, 13363, replace = FALSE)
trainCOVID <- dataCOVID[train,]
testCOVID <- dataCOVID[-train,]
school.fit <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                    ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)

plot(school.fit)
summary(school.fit)
print(school.fit)

school.vimp <- vimp(school.fit, importance= "permute")

school.pred <- predict(school.fit, testCOVID, na.action = "na.omit", importance=TRUE)

#VIMPs from 20 iterations of random survival forests
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[,22:70], na.action = "na.omit", 
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.1 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[,22:70], na.action = "na.omit", 
                      ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.2 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
    school.vimp.3 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.4 <- vimp(school.rf, importance= "permute") 
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
    school.vimp.5 <- vimp(school.rf, importance= "permute") 
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.6 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.7 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.8 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.9 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.10 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.11 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.12 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.13 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.14 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.15 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.16 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.17 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.18 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.19 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, school_closure_Unesco)~ ., data = dataCOVID[, 22:70], na.action = "na.omit",
                        ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
   school.vimp.20 <- vimp(school.rf, importance= "permute")

#create somewhere to store importance values for each of 20 iterations 
importancemat <- matrix(nrow=ncol(dataCOVID[,22:70])-2,ncol=20)

vector1 <- unlist(school.vimp.1$importance)
importancemat[,1] <- vector1
vector2 <- unlist(school.vimp.2$importance)
importancemat[,2] <- vector2
vector3 <- unlist(school.vimp.3$importance)
importancemat[,3] <- vector3
vector4 <- unlist(school.vimp.4$importance)
importancemat[,4] <- vector4
vector5 <- unlist(school.vimp.5$importance)
importancemat[,5] <- vector5
vector6 <- unlist(school.vimp.6$importance)
importancemat[,6] <- vector6
vector7 <- unlist(school.vimp.7$importance)
importancemat[,7] <- vector7
vector8 <- unlist(school.vimp.8$importance)
importancemat[,8] <- vector8
vector9 <- unlist(school.vimp.9$importance)
importancemat[,9] <- vector9
vector10 <- unlist(school.vimp.10$importance)
importancemat[,10] <- vector10
vector11<- unlist(school.vimp.11$importance)
importancemat[,11] <- vector11
vector12 <- unlist(school.vimp.12$importance)
importancemat[,12] <- vector12
vector13<- unlist(school.vimp.13$importance)
importancemat[,13] <- vector13
vector14<- unlist(school.vimp.14$importance)
importancemat[,14] <- vector14
vector15<- unlist(school.vimp.15$importance)
importancemat[,15] <- vector15
vector16<- unlist(school.vimp.16$importance)
importancemat[,16] <- vector16
vector17<- unlist(school.vimp.17$importance)
importancemat[,17] <- vector17
vector18<- unlist(school.vimp.18$importance)
importancemat[,18] <- vector18
vector19<- unlist(school.vimp.19$importance)
importancemat[,19] <- vector19
vector20<- unlist(school.vimp.20$importance)
importancemat[,20] <- vector20

importance.names <- names(dataCOVID[,24:70])
importance.data <- data.frame(var.name = importance.names, importancemat)
#Average importance for each variable across imputations
importance.score <- apply(importancemat, 1, mean)
importance.data$mean <- importance.score
View(importance.data)
write.csv(importance.data, "RSF_March2023_school.csv")



##Use Random Forest SRC package to estimate random forest survival regression with bootstrapping built in

forest_school <- rfsrc(fitform, data = dataCOVID[, 14:93], ntree = 100, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)

## 

pcox <- predictSurvProb(fitcox, dataCOVID)
prsf <- predictSurvProb(fitrsf, newdata = newData)
pcf <- predictSurvProb(fitcforest, newdata = newData)
psp <- round(cbind(pcox,prsf, pcf)*100,2)
newNames <- paste("\\code{newData}", 1:3)
mat <- cbind(newNames, newData$age, psp)
colnames(mat) <- c("Id", "Age", "selected Cox regression", "randomSurvivalForest", "cforest")
print(mat)

#################################################################################################
rm(list=ls())

dataCOVID <- read.csv("C:/Users/kaish/Documents/Academic Work/Writing/Covid Policy Diffusion/DataCOVID_randomforest_March2023_domestic.csv")

dataCOVID$des_school[dataCOVID$des_school %in% NA] <- 0
dataCOVID$des_work[dataCOVID$des_work %in% NA] <- 0
dataCOVID$des_public[dataCOVID$des_public %in% NA] <- 0
dataCOVID$des_domestic[dataCOVID$des_domestic %in% NA] <- 0
dataCOVID$des_international[dataCOVID$des_international %in% NA] <- 0

fitform <- Surv(time = dataCOVID$tf, event = dataCOVID$des_domestic) ~ dataCOVID$WHO_pandemic + dataCOVID$US_domestic + dataCOVID$China_domestic + dataCOVID$FDI_US + dataCOVID$BRI_country + 
  dataCOVID$GHI_prime + dataCOVID$airmil + dataCOVID$pop_older65 + dataCOVID$World_domestic + dataCOVID$region_domestic + dataCOVID$World_casethou + dataCOVID$World_deaththou + 
  dataCOVID$V4_Bin + dataCOVID$V8_Bin + dataCOVID$v2exl_legitideol + dataCOVID$v2exl_legitideolcr_0 + dataCOVID$v2exl_legitideolcr_1 + dataCOVID$v2exl_legitideolcr_2 + dataCOVID$v2exl_legitideolcr_3 + dataCOVID$v2exl_legitideolcr_4 + 
  dataCOVID$elections2020 + dataCOVID$v2x_freexp + dataCOVID$v2dlengage + dataCOVID$polyarchy_stddev + dataCOVID$COVID_casesthou + dataCOVID$v2x_polyarchy + dataCOVID$GDPmil + dataCOVID$HDI + dataCOVID$areamil + dataCOVID$popmil + 
  dataCOVID$v2xel_locelec + dataCOVID$v2xel_regelec + dataCOVID$women_in_parliament

fitcox <- coxph(fitform)
summary(fitform)

## Replace NAs in the des_ columns
dataCOVID <- 
  dataCOVID %>% 
  mutate(across(starts_with("des_"), ~replace_na(.x, 0)),
         tf = tf+67) # this line makes the minimum value of tf 0. Needs to be checked if that makes sense

## Replace NAs in the des_ columns
dataCOVID <- 
  dataCOVID %>% mutate(across(starts_with("des_"), ~replace_na(.x, 0))) %>% 
  filter(tf >-1)

dataCOVID <- dataCOVID %>% filter(tf >-1)

## Random forest
set.seed(1000)
train <- sample(1:19090, 13363, replace = FALSE)
trainCOVID <- dataCOVID[train,]
testCOVID <- dataCOVID[-train,]
school.fit <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                    ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)

plot(school.fit)
summary(school.fit)
print(school.fit)

school.vimp <- vimp(school.fit, importance= "permute")

school.pred <- predict(school.fit, testCOVID, na.action = "na.omit", importance=TRUE)

#VIMPs from 20 iterations of random survival forests
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.1 <- vimp(school.rf, importance= "permute")

school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.2 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.3 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.4 <- vimp(school.rf, importance= "permute") 
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.5 <- vimp(school.rf, importance= "permute") 
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.6 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.7 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.8 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.9 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.10 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.11 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.12 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.13 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.14 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.15 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.16 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.17 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.18 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.19 <- vimp(school.rf, importance= "permute")
school.rf <- rfsrc(Surv(tf, S6_numdomestic)~ ., data = dataCOVID[, 22:64], na.action = "na.omit",
                   ntree = 750, mtry = 5, bootstrap = c("by.root"), samptype = c("swor"), do.trace = TRUE)
school.vimp.20 <- vimp(school.rf, importance= "permute")

#create somewhere to store importance values for each of 20 iterations 
importancemat <- matrix(nrow=ncol(dataCOVID[,22:64])-2,ncol=20)

vector1 <- unlist(school.vimp.1$importance)
importancemat[,1] <- vector1
vector2 <- unlist(school.vimp.2$importance)
importancemat[,2] <- vector2
vector3 <- unlist(school.vimp.3$importance)
importancemat[,3] <- vector3
vector4 <- unlist(school.vimp.4$importance)
importancemat[,4] <- vector4
vector5 <- unlist(school.vimp.5$importance)
importancemat[,5] <- vector5
vector6 <- unlist(school.vimp.6$importance)
importancemat[,6] <- vector6
vector7 <- unlist(school.vimp.7$importance)
importancemat[,7] <- vector7
vector8 <- unlist(school.vimp.8$importance)
importancemat[,8] <- vector8
vector9 <- unlist(school.vimp.9$importance)
importancemat[,9] <- vector9
vector10 <- unlist(school.vimp.10$importance)
importancemat[,10] <- vector10
vector11<- unlist(school.vimp.11$importance)
importancemat[,11] <- vector11
vector12 <- unlist(school.vimp.12$importance)
importancemat[,12] <- vector12
vector13<- unlist(school.vimp.13$importance)
importancemat[,13] <- vector13
vector14<- unlist(school.vimp.14$importance)
importancemat[,14] <- vector14
vector15<- unlist(school.vimp.15$importance)
importancemat[,15] <- vector15
vector16<- unlist(school.vimp.16$importance)
importancemat[,16] <- vector16
vector17<- unlist(school.vimp.17$importance)
importancemat[,17] <- vector17
vector18<- unlist(school.vimp.18$importance)
importancemat[,18] <- vector18
vector19<- unlist(school.vimp.19$importance)
importancemat[,19] <- vector19
vector20<- unlist(school.vimp.20$importance)
importancemat[,20] <- vector20

importance.names <- names(dataCOVID[,24:64])
importance.data <- data.frame(var.name = importance.names, importancemat)
#Average importance for each variable across imputations
importance.score <- apply(importancemat, 1, mean)
importance.data$mean <- importance.score
View(importance.data)
write.csv(importance.data, "RSF_March2023_domestic.csv")

#######################################################
